﻿using HalconDotNet;
using Halcon二次开发.Model;
using Halcon二次开发.Model.储放所有单例.储存每个单例界面;
using Halcon二次开发.View.定位跟随;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Halcon二次开发.Controller.定位跟随
{
    public class FixtrueController
    {
        private HTuple hv_HomMat2DIdentity1, hv_HomMat2DRotate1, hv_HomMat2DTranslate1;
        private HObject ho_ImageAffineTrans;
        private HSmartWindowControl smartWindowControl;
        /// <summary>
        /// 当前图像对象(输入)
        /// </summary>
        public HObject IntputImage;
        /// <summary>
        /// 这些都是模板匹配需要传入的参数，角度，中心点的位置
        /// 
        /// InputStandardRow,InputStandardColumn是基准位置
        /// </summary>
        
        public double IntputRow;
        public double IntputColumn;
        public double IntputPhi;
        public double IntputStandardRow;
        public double IntputStandardColumn;
        /// <summary>
        /// 定位跟随处理后的图像
        /// </summary>
        public HObject OutputImage;
        /// <summary>
        /// 当前界面的对象
        /// </summary>
        private FixtureToolFForm form { get; set; }
        public FixtrueController(FixtureToolFForm form)
        {
            smartWindowControl = StaicResouce.HwindowFlowNodeUrse.hSmartWindowControl1;
            HOperatorSet.GenEmptyObj(out ho_ImageAffineTrans);
            this.form = form;
        }
        /// <summary>
        /// 实现定位跟随功能
        /// hv_shapeRow,hv_shapeColumn调整位置x，y
        /// </summary>
        /// 
        public void Run()
        {
            //先清除原图层
            HOperatorSet.ClearWindow(smartWindowControl.HalconWindow);
            try
            {
                //******************************图像仿射,使物料回原摆正
                HOperatorSet.HomMat2dIdentity(out hv_HomMat2DIdentity1);
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    HOperatorSet.HomMat2dRotate(hv_HomMat2DIdentity1, 0 - IntputPhi, IntputRow, IntputColumn,
                        out hv_HomMat2DRotate1);
                }
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    HOperatorSet.HomMat2dTranslate(hv_HomMat2DRotate1, (IntputStandardRow + (int)form.numericUpDown1.Value) - IntputRow, (IntputStandardColumn + (int)form.numericUpDown2.Value) - IntputColumn,
                        out hv_HomMat2DTranslate1);
                }
                HOperatorSet.AffineTransImage(IntputImage, out ho_ImageAffineTrans, hv_HomMat2DTranslate1,
                    "constant", "false");
                HOperatorSet.DispObj(ho_ImageAffineTrans, smartWindowControl.HalconWindow);
                OutputImage = ho_ImageAffineTrans;
            }
            catch
            {
                throw new Exception("定位跟随运行失败，请检查你的模板或计量模型是否正确!");
            }
        }
    }
}